package com.bw.a1908.core.mvp;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public
class BasePresenter<M extends IModel, V extends IView> implements IPresenter {

    protected M mModel;
    protected V mView;
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();

    public BasePresenter(M mModel, V mView) {
        this.mModel = mModel;
        this.mView = mView;
    }

    /**
     * 添加订阅关系
     */
    public void addDisposable(Disposable disposable) {
        compositeDisposable.add(disposable);
    }

    @Override
    public void destroy() {
        //RxJava防止内存泄漏
        if (compositeDisposable.size() > 0) {
            compositeDisposable.dispose();
            compositeDisposable = null;
        }
        if (mModel != null) {
            mModel.destroy();
            mModel = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
